package org.gnyxy.controller.background;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.DishDTO;
import org.gnyxy.dto.PageQueryDTO;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.DishService;
import org.gnyxy.vo.DishWithInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController("AdminDishController")
@RequestMapping("/background/dish")
@Slf4j
@Api(tags = "商家后台-菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    public static final String[] CacheNames = {"dishCache", "dishInfoCache"};



    /**
     * 添加菜品
     * @param dishDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加菜品")
    @CacheEvict(cacheNames = {"dishCache", "dishInfoCache"}, allEntries = true)
    public Result add(@RequestBody DishDTO dishDTO){
        log.info("添加的菜品为: {}",dishDTO);
        dishService.add(dishDTO);
        return Result.success();
    }

    /**
     * 根据id删除菜品
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("根据id删除菜品")
    @CacheEvict(cacheNames = {"dishCache", "dishInfoCache"}, allEntries = true)
    public Result delete(@PathVariable Long id){
        log.info("删除菜品id: {}",id);
        dishService.delete(id);
        return Result.success();
    }

    /**
     * 根据id获取菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取菜品")
    public Result<DishWithInfoVO> getById(@PathVariable Long id){
        log.info("获取菜品，id为: {}",id);
        DishWithInfoVO dishWithInfoVO = dishService.getById(id);
        return Result.success(dishWithInfoVO);
    }

    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新菜品")
    @CacheEvict(cacheNames = {"dishCache", "dishInfoCache"}, allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("更新菜品: {}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(PageQueryDTO pageQueryDTO){
        log.info("分页查询: {}",pageQueryDTO);
        PageResult result = dishService.page(pageQueryDTO);
        return Result.success(result);
    }

    /**
     * 设置状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置状态")
    @CacheEvict(cacheNames = {"dishCache", "dishInfoCache"}, allEntries = true)
    public Result setStatus(@PathVariable Integer status,Long id){
        log.info("设置菜品id: {},状态为: {}",id,status.equals(StatusConstant.ENABLE) ? "启用":"禁用");
        dishService.setStatus(status,id);
        return Result.success();
    }

    /**
     * 自定义菜品价格
     * @param price
     * @return
     */
    @PostMapping("/price/{price}")
    @ApiOperation("自定义菜品价格")
    @CacheEvict(cacheNames = {"dishCache", "dishInfoCache"}, allEntries = true)
    public Result setPrice(@PathVariable BigDecimal price, Long id){
        log.info("自定义菜品价格: {}",price);
        dishService.setPrice(price,id);
        return Result.success();
    }
}
